﻿using System;
using System.Windows.Threading;
using Caliburn.Micro;
using FinalCountdownServices;
using FinalCountdownServices.Models;
using Microsoft.Phone.Tasks;

namespace FinalCountdown.ViewModels
{
    public class EventPageViewModel : Screen
    {
        public int EventId { get; set; }

        private EventModel myEvent;
        public EventModel MyEvent
        {
            get { return myEvent; }
            set
            {
                myEvent = value;
            }
        }


        public string Title
        {
            get { return MyEvent == null ? "" : myEvent.Title; }

        }
        public string Description
        {
            get { return MyEvent == null ? "" : MyEvent.Description; }
        }
        public bool ShowTimer { get { return MyEvent != null && !MyEvent.Obsolete; } }
        public bool Obsolete { get { return MyEvent != null && MyEvent.Obsolete; } }

        public int RemainingDays { get { return MyEvent == null ? 0 : MyEvent.RemainingDays; } }
        public int RemainingHours { get { return MyEvent == null ? 0 : MyEvent.RemainingHours; } }
        public int RemainingMinutes { get { return MyEvent == null ? 0 : MyEvent.RemainingMinutes; } }
        public int RemainingSeconds { get { return MyEvent == null ? 0 : MyEvent.RemainingSeconds; } }


        private IEventService _eventService;
        private ITileService _tileService;
        private DispatcherTimer myTimer;


        public EventPageViewModel(ITileService tileService, IEventService eventService)
        {
            _eventService = eventService;
            _tileService = tileService;
            myTimer = new DispatcherTimer();
            _tileService.UpdateAllAndReSchedule(_eventService.MyEvents);

        }

        protected override void OnActivate()
        {
            Init();
        }

        public void Init()
        {
            myEvent = _eventService.GetEvent(EventId);
            NotifyOfPropertyChange(() => Title);
            NotifyOfPropertyChange(() => Description);
            NotifyOfPropertyChange(() => ShowTimer);
            NotifyOfPropertyChange(() => Obsolete);
            StartTimer();
        }

        private void StartTimer()
        {

            myTimer.Interval = TimeSpan.FromSeconds(1);
            myTimer.Tick += (s, e) => Notify();
            myTimer.Start();
        }

        protected override void OnDeactivate(bool close)
        {
            myTimer.Stop();
        }


        private void Notify()
        {
            NotifyOfPropertyChange(() => RemainingDays);
            NotifyOfPropertyChange(() => RemainingHours);
            NotifyOfPropertyChange(() => RemainingMinutes);
            NotifyOfPropertyChange(() => RemainingSeconds);
        }


        public void ShareOnFaceBook()
        {
            myTimer.Stop();

            var sst = new ShareStatusTask();
            sst.Status = "Használtam a Final Countdown alkalmazást!";
            sst.Show();
        }


    }
}
